package org.wombat.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Properties;

/**
 * Created by IntelliJ IDEA.
 * User: rovsipet
 * Date: 23-11-2010
 * Time: 09:48:30
 * To change this template use File | Settings | File Templates.
 */
public class SystemProperties {


    private static SystemProperties instance;
    private Properties labels = null;
    Logger logNormal = LoggerFactory.getLogger(LoggerNames.LOGGER_INITIALIZATION);
    Logger logError = LoggerFactory.getLogger(LoggerNames.LOGGER_ERROR);

    public SystemProperties() {
        labels = loadPropertyFile();
    }

    public String getProperty(String key) {
        if(labels.containsKey(key))
            return labels.getProperty(key);
        else
            return null;
    }

    private Properties loadPropertyFile() {
        Properties props = new Properties();
        BufferedReader input = null;

        String path = System.getenv(SystemConstants.SYSTEM_ENVIRONMENT_NAME);
        if(path != null)
            try {
                logNormal.info("Loading label properties file from '" + path + "'");
                StringBuilder filePath = buildFilePath(path);

                input =  new BufferedReader(new FileReader(filePath.toString()));
                props.load(input);
            }
            catch (Exception e){
                logNormal.error("An error accured when loading the label property file, see the error log for details.");
                logError.error(e.toString());
                for(StackTraceElement st : e.getStackTrace())
                    logError.error(st.toString());
            }finally{
                if (input != null)
                    try{
                        input.close();
                    } catch (Exception e) {
                        logNormal.error("An error accured when closing the file input stream, see the error log for details.");
                        logError.error(e.toString());
                        for(StackTraceElement st : e.getStackTrace())
                            logError.error(st.toString());
                    }
            }
        else
            logNormal.warn(new StringBuilder("System environment variable ").append(SystemConstants.SYSTEM_ENVIRONMENT_NAME).append(" not defined. No labels loaded").toString());

        if( ! props.isEmpty() )
            logNormal.info(props.size() + " labels loaded from " + SystemConstants.SYSTEM_PROPERTY_FILENAME  + ".");

        return props;
    }

    private StringBuilder buildFilePath(String path) {
        StringBuilder filePath = new StringBuilder(path);

        char lastChar = filePath.charAt(filePath.length() -1);
        boolean endsWithSlashValue = lastChar == '/' || lastChar == '\\';

        if(endsWithSlashValue)
            filePath.append(SystemConstants.SYSTEM_PROPERTY_FILENAME);
        else
            filePath.append('/').append(SystemConstants.SYSTEM_PROPERTY_FILENAME);
        return filePath;
    }
}
